﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Configuration;
using Business.DataBusiness;
using Business;

namespace MuaBanDT
{
    public partial class QuanLy : Form
    {
        Utilities getData = new Utilities();
        public QuanLy()
        {
            InitializeComponent();
        }

        private void btnThongKeBan_Click(object sender, EventArgs e)
        {
            if (this.Ngay.Checked == true)
            {
              this.dtViewTKBan.DataSource = getData.DataHangBanTheoNgay(this.dtThongKeBan.Value.Date.ToShortDateString());
            }
            else
            {
                this.dtViewTKBan.DataSource = getData.DataHangBanTheoThang(this.dtThongKeBan.Value.Date.ToShortDateString());
            }
        }

        private void btnThongKeT_Click(object sender, EventArgs e)
        {
            MucDT data = new MucDT();
            this.dtViewTon.DataSource = data.DataHangTon();
        }

        private void QuanLy_Load(object sender, EventArgs e)
        {
            this.cBoxMaNVDH.Items.AddRange(getData.GetMaNV("QL"));
            this.cBoxMaDT.Items.AddRange(getData.GetDanhMucMaDT("DanhMucDT"));
            this.dtViewCapNhatGia.DataSource = getData.GetDataTable("ThongTinGiaBan");
            this.dtViewDatH.DataSource = getData.GetDataTable("ThongTinDatHang");
            btnXoaCNG_Click(sender, e);
            this.tBoxSoLuong.Text = "0";
        }

        private void btnXoaCNG_Click(object sender, EventArgs e)
        {
            this.cBoxMaDT.Text = "";
            this.tBoxGiaCu.Text = "";
            this.tBoxGiaMoi.Text = "";
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                ThongTinGiaBan a = new ThongTinGiaBan();
                a.MaDT = this.cBoxMaDT.Text;
                a.GiaBan = int.Parse(this.tBoxGiaMoi.Text);
                a.Update();
                this.dtViewCapNhatGia.DataSource = getData.GetDataTable("ThongTinGiaBan");
                this.tBoxGiaMoi.Text = "";
            }
            catch { }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            XoaDH();
        }
        private void XoaDH()
        {
            this.tBoxMaDH.Text = "";
            this.tBoxMaDTD.Text = "";
            this.tBoxSoLuong.Text = "";
            this.tBoxDonViTinh.Text = "";
            this.cBoxMaNVDH.Text = "";
            this.dtimeNgayDat.Value = DateTime.Now;
        }
          private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DatHang a = new DatHang();
                a.MaDH= getData.CreateID("ThongTinDatHang","DH");
                a.MaNV = this.cBoxMaNVDH.Text;
                a.MaDT = this.tBoxMaDTD.Text;
                a.DonViTinh=this.tBoxDonViTinh.Text;
                a.SoLuong= int.Parse(this.tBoxSoLuong.Text);
                a.NgayDH= DateTime.Parse(this.dtimeNgayDat.Value.ToShortDateString());
                tBoxMaDH.Text = a.MaDH;
                if (a.Insert() == 1)
                {
                    MessageBox.Show("Lưu thành công", "Đặt Hàng", MessageBoxButtons.OK);
                    this.dtViewDatH.DataSource = getData.GetDataTable("ThongTinDatHang");
                }
               
            }
            catch(Exception ex) 
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
       private void btnSuaDat_Click(object sender, EventArgs e)
        {
            try
            {
                DatHang a = new DatHang();
                a.MaDH = this.tBoxMaDH.Text;
                a.MaNV = this.cBoxMaNVDH.Text;
                a.MaDT = this.tBoxMaDTD.Text;
                a.DonViTinh = this.tBoxDonViTinh.Text;
                a.SoLuong = int.Parse(this.tBoxSoLuong.Text);
                a.NgayDH = DateTime.Parse(this.dtimeNgayDat.Value.ToShortDateString());
                a.Update();
                this.dtViewDatH.DataSource = getData.GetDataTable("ThongTinDatHang");
            }
            catch { }
        }

        private void dtViewCapNhatGia_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.cBoxMaDT.Text = getData.Value("ThongTinGiaBan",e.RowIndex,0);
                this.tBoxGiaCu.Text = getData.Value("ThongTinGiaBan", e.RowIndex,1);
            }
            catch 
            {
                this.cBoxMaDT.Text = "";
                 this.tBoxGiaCu.Text = "";
                 this.tBoxGiaMoi.Text = "";
            }
        }

        private void dtViewDatH_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.tBoxMaDH.Text = getData.Value("ThongTinDatHang", e.RowIndex, 0);
                this.cBoxMaNVDH.Text = getData.Value("ThongTinDatHang", e.RowIndex, 1);
                this.tBoxMaDTD.Text = getData.Value("ThongTinDatHang", e.RowIndex, 2);
                this.tBoxDonViTinh.Text = getData.Value("ThongTinDatHang", e.RowIndex, 3);
                this.tBoxSoLuong.Text = getData.Value("ThongTinDatHang", e.RowIndex, 4);
                this.dtimeNgayDat.Text = getData.Value("ThongTinDatHang", e.RowIndex, 5);
            }
            catch
            {
                XoaDH();
            }
              
           
        }
        private void dtViewDatH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 46)
            {
                if (MessageBox.Show(" Xoa thong tin dat hang vua chon", "Dat Hang", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DatHang a = new DatHang();
                    a.Delete_ThongTinDatHang(this.tBoxMaDH.Text.ToString());
                    this.dtViewDatH.DataSource = getData.GetDataTable("ThongTinDatHang");
                }
                
            }
        }

        private void tBoxGiaMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Bạn chỉ được nhập số", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Handled = true;
            }
        }

        private void tBoxSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            tBoxGiaMoi_KeyPress(sender, e);
        }
          
      }
}
